package com.zy.cat.controller.mall.v1;

import java.util.List;
import java.util.concurrent.Callable;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.zy.cat.entity.pdd.PddShops;
import com.zy.cat.service.JdShopsService;
import com.zy.cat.common.annotation.CheckDevPermission;
import com.zy.cat.common.annotation.CheckingMD5;
import com.zy.cat.entity.jd.JDGoods;
import com.zy.cat.entity.jd.JDGoodsInfo;
import com.zy.cat.entity.jd.JDJFGoods;
import com.zy.cat.entity.mall.resp.TKSelectionResp;
import com.zy.cat.entity.pdd.PddShopConvert;
import com.zy.cat.entity.resp.Pages;
import com.zy.cat.entity.resp.RSPE;
import com.zy.cat.entity.resp.ResultPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jd.union.open.category.goods.get.response.CategoryResp;
import jd.union.open.promotion.bysubunionid.get.response.PromotionCodeResp;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 * @info 
 * @author Link
 * @date 2020-07-01 10:07
 */
@Api(value = "jd_shops", tags = { "9趣猫京东商城开放API" }, description = "9趣猫京东商城开放接")
@RestController
@RequestMapping("/v1/jd/goods")
@Slf4j
public class JDShopController {
	@Autowired
	JdShopsService jdShopsService;
	
	@PostMapping("/jxlist")
	@ApiOperation(value = "京东联盟精选优质商品", notes = "京东联盟精选优质商品", position = 1, response = JDJFGoods.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "eid", value = "频道id：1-好券商品,2-超级大卖场,10-9.9专区,22-热销爆品,23-为你推荐,24-数码家电,25-超市,26-母婴玩具,27-家具日用,28-美妆穿搭,29-医药保健,30-图书文具,31-今日必推,32-品牌好货,33-秒杀商品,34-拼购商品,40-高收益,41-自营热卖榜,109-新品首发,110-自营,125-首购商品,129-高佣榜单,130-视频商品", example = "1", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "stype", value = "排序字段(0：单价, 1：佣金比例, 2：佣金， 3维度30天引单量，4：评论数，5：好评数)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "sort", value = "asc,desc升降序,默认降序", example = "desc", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_eid_stype_sort_uuid_sort_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_eid_stype_sort_uuid_sort", MD5 = "token")
	public Object jxList(Integer rtype, Integer eid, @RequestParam(name ="stype",defaultValue = "5" )Integer stype, @RequestParam(name ="sort",defaultValue = "desc" )String sort, String uuid,
			String token, Pages pages) {
		if (rtype == null) {
			return new RSPE<ResultPage<PddShops>>(-2, "应用平台参数错误");
		}
		if (pages == null || pages.getPageSize() > 50 || pages.getPageSize() < 1) {
			return new RSPE<Object>(-3, "不在分页范围[1~50]");
		}
		if (eid == null||stype == null||sort == null||!"asc,desc".contains(sort)) {
			return new RSPE<Object>(-4, "必要参数错误");
		}
		
		Callable<RSPE<ResultPage<JDJFGoods>>> result = new Callable<RSPE<ResultPage<JDJFGoods>>>() {
			@Override
			public RSPE<ResultPage<JDJFGoods>> call() throws Exception {
				return new RSPE<ResultPage<JDJFGoods>>(jdShopsService.jxList(rtype, eid, stype, sort, pages));
			}
		};
		return result;
	}

	@PostMapping("/list")
	@ApiOperation(value = "获取商品信息/商品类似推荐", notes = "根据类目获取商品信息(根据首页商品分类类API获取 )", position = 1, response = JDGoods.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ctype", value = "是否有优惠券(-1.全部、0.无、1.有)", example = "-1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "cid1", value = "1级类目ID", example = "1316", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "cid2", value = "2级类目ID", example = "1387", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "cid3", value = "3级类目ID", example = "1425", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "stype", value = "排序字段(0：单价, 1：佣金比例, 2：佣金， 3：30天引单量， 4：30天支出佣金)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "sort", value = "asc,desc升降序,默认降序", example = "desc", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_cid1_cid2_cid3_uuid_rtype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_cid1_cid2_cid3_uuid_rtype", MD5 = "token")
	public Object list(@RequestParam(name ="ctype",defaultValue = "1" )Integer ctype,Integer rtype, Long cid1, Long cid2, Long cid3, String uuid,
			String token, @RequestParam(name ="stype",defaultValue = "0" )Integer stype, @RequestParam(name ="sort",defaultValue = "desc" )String sort, Pages pages) {
		//排序字段(0：单价, 1：佣金比例, 2：佣金， 3：30天引单量， 4：30天支出佣金)
		if (rtype == null) {
			return new RSPE<ResultPage<PddShops>>(-2, "应用平台参数错误");
		}
		if (pages == null || pages.getPageSize() > 30 || pages.getPageSize() < 10) {
			return new RSPE<Object>(-3, "不在分页范围[10~30]");
		}
		if (cid1 == null&&cid2 == null&&cid3 == null) {
			return new RSPE<ResultPage<JDGoods>>(-4, "分类ID参数错误");
		}
		if ( stype == null||sort == null||!"asc,desc".contains(sort)) {
			return new RSPE<Object>(-5, "排序必要参数错误");
		}
		Callable<RSPE<ResultPage<JDGoods>>> result = new Callable<RSPE<ResultPage<JDGoods>>>() {
			@Override
			public RSPE<ResultPage<JDGoods>> call() throws Exception {
				return new RSPE<ResultPage<JDGoods>>(jdShopsService.list(rtype, cid1, cid2, cid3,stype,sort, pages,ctype));
			}
		};
		return result;
	}
	
	@PostMapping("/cats")
	@ApiOperation(value = "商品类目信息", notes = "商品类目列表同(根据首页商品分类类API获取 )", position = 2, response = CategoryResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "parent", value = "父类目id(一级父类目为0)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "grade", value = "类目级别(类目级别 0，1，2 代表一、二、三级类目)", example = "0", dataType = "int", paramType = "form"),
  			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_parent_parent_grade_uuid_grade_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_parent_parent_grade_uuid_grade", MD5 = "token")
	public Object cats(Integer rtype, Integer parent, Integer grade, String uuid,
			String token ) {
		if (rtype == null) {
			return new RSPE<ResultPage<PddShops>>(-2, "应用平台参数错误");
		}
		if (parent == null || grade==null) {
			return new RSPE<Object>(-3, "类目信息错误");
		}
		 
		Callable<RSPE<List<CategoryResp>>> result = new Callable<RSPE<List<CategoryResp>>>() {
			@Override
			public RSPE<List<CategoryResp>> call() throws Exception {
				return new RSPE<List<CategoryResp>>(jdShopsService.catList(rtype, parent, grade));
			}
		};
		return result;
	}
	
	@PostMapping("/search")
	@ApiOperation(value = "商品搜索", notes = "商品搜索", position = 2, response = JDGoods.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "ctype", value = "是否有优惠券(-1.全部、0.无、1.有)", example = "-1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "owner", value = "商品类型：自营[g]，POP[p]", example = "自营[g]", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "query", value = "搜索关键词", example = "女鞋",required = true, dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "stype", value = "排序字段(0：单价, 1：佣金比例, 2：佣金， 3：30天引单量， 4：30天支出佣金)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "sort", value = "asc,desc升降序,默认降序", example = "desc", dataType = "String", paramType = "form"),
 			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_rtype_uuid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_rtype_uuid", MD5 = "token")
	public Object search(Integer rtype, @RequestParam(name ="ctype",defaultValue = "1" )Integer ctype, String owner, String query, @RequestParam(name ="stype",defaultValue = "0" )Integer stype, @RequestParam(name ="sort",defaultValue = "desc" )String sort,  String uuid,
			String token, Pages pages) {
		if (rtype == null) {
			return new RSPE<ResultPage<JDGoods>>(-2, "应用平台参数错误");
		}
		if (pages == null || pages.getPageSize() > 30 || pages.getPageSize() < 10) {
			return new RSPE<Object>(-3, "不在分页范围[10~30]");
		}
		if ( stype == null||sort == null||!"asc,desc".contains(sort)) {
			return new RSPE<Object>(-4, "排序必要参数错误");
		}

		Callable<RSPE<ResultPage<JDGoods>>> result = new Callable<RSPE<ResultPage<JDGoods>>>() {
			@Override
			public RSPE<ResultPage<JDGoods>> call() throws Exception {
				return new RSPE<ResultPage<JDGoods>>(jdShopsService.search(rtype, query, owner, pages,sort,stype,ctype));
			}
		};
		return result;
	}

	 

	@PostMapping("/detail")
	@ApiOperation(value = "获取商品详细信息", notes = "获取商品详细信息", position = 4, response = JDGoodsInfo.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "skuid", value = "商品ID", example = "71094795011",required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_skuid_rtype_skuid_uuid_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_skuid_rtype_skuid_uuid", MD5 = "token")
	public Object shopDetail(Integer rtype, String uuid, String token, String skuid) {
		if (rtype == null) {
			return new RSPE<Object>(-2, "应用平台参数错误");
		}
		if (skuid == null) {
			return new RSPE<Object>(-3, "sid错误");
		}
		Callable<RSPE<JDGoodsInfo>> result = new Callable<RSPE<JDGoodsInfo>>() {
			@Override
			public RSPE<JDGoodsInfo> call() throws Exception {
				return new RSPE<JDGoodsInfo>(jdShopsService.info(rtype, skuid));
			}
		};
		return result;
	}

	@PostMapping("/convert")
	@ApiOperation(value = "商品高佣转链", notes = "商品高佣转链", position = 5, response = PromotionCodeResp.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "atype", value = "账户(0.九趣猫账户)", example = "0", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "pid", value = "用户ID", example = "0", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "mid", value = "商品落地页", example = "https://wqitem.jd.com/XXX", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "cid", value = "商品优惠券链接", example = "http://coupon.jd.com/XXX", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "ctype", value = "APP平台(0.九趣猫、1.淘淘集、2.一陶)", example = "1", required = true, dataType = "int", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "utoken", value = "登录返回字段", example = "1001", required = true, dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "token", value = "MD5(rtype_pid_ctype_uuid_atype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
	@CheckingMD5(checkingStr = "rtype_pid_ctype_uuid_atype", MD5 = "token")
	@CheckDevPermission
	public Object shopConvert(@RequestParam(name = "atype",defaultValue = "0")Integer atype,@RequestParam(name = "ctype",defaultValue = "0")Integer ctype,Integer rtype, String pid, String mid,String cid, String uuid, String token,
			String utoken ) {
		if (rtype == null||ctype == null) {
			return new RSPE<Object>(-1, "平台参数错误");
		}
		if (StringUtils.isBlank(mid)) {
			return new RSPE<Object>(-2, "mid参数错误");
		}
		Callable<RSPE<PromotionCodeResp>> result = new Callable<RSPE<PromotionCodeResp>>() {
			@Override
			public RSPE<PromotionCodeResp> call() throws Exception {
				return new RSPE<PromotionCodeResp>(jdShopsService.convert(atype,ctype,rtype, pid, mid,cid ));
			}
		};
		return result;
	}
	
	@PostMapping("/today/tj")
	@ApiOperation(value = "获取今日推荐商品信息", notes = "获取今日推荐商品信息",position = 2, response = JDGoods.class)
	
	@ApiImplicitParams({
		@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
		@ApiImplicitParam(name = "ctype", value = "是否有优惠券(-1.全部、0.无、1.有)", example = "-1", required = true, dataType = "int", paramType = "form"),
		@ApiImplicitParam(name = "stype", value = "排序字段(0：单价, 1：佣金比例, 2：佣金， 3：30天引单量， 4：30天支出佣金)", example = "0", dataType = "int", paramType = "form"),
		@ApiImplicitParam(name = "sort", value = "asc,desc升降序,默认降序", example = "desc", dataType = "String", paramType = "form"),
			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
		@ApiImplicitParam(name = "token", value = "MD5(rtype_uuid_rtype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
@CheckingMD5(checkingStr = "rtype_uuid_rtype", MD5 = "token")
public Object tjList(@RequestParam(name ="ctype",defaultValue = "1" )Integer ctype,Integer rtype, String uuid,
		String token, @RequestParam(name ="stype",defaultValue = "0" )Integer stype, @RequestParam(name ="sort",defaultValue = "desc" )String sort, Pages pages) {
	//排序字段(0：单价, 1：佣金比例, 2：佣金， 3：30天引单量， 4：30天支出佣金)
	if (rtype == null) {
		return new RSPE<ResultPage<PddShops>>(-2, "应用平台参数错误");
	}
	if (pages == null || pages.getPageSize() > 30 || pages.getPageSize() < 10) {
		return new RSPE<Object>(-3, "不在分页范围[10~30]");
	}
	if ( stype == null||sort == null||!"asc,desc".contains(sort)) {
		return new RSPE<Object>(-5, "排序必要参数错误");
	}
	Callable<RSPE<ResultPage<JDGoods>>> result = new Callable<RSPE<ResultPage<JDGoods>>>() {
		@Override
		public RSPE<ResultPage<JDGoods>> call() throws Exception {
			return new RSPE<ResultPage<JDGoods>>(jdShopsService.getTJList(rtype,stype,sort, pages,ctype));
		}
	};
	return result;
}
//	@ApiImplicitParams({
//			@ApiImplicitParam(name = "rtype", value = "应用平台(0.安卓、1.IOS)", example = "0", required = true, dataType = "int", paramType = "form"),
//			@ApiImplicitParam(name = "sex", value = "性别0.女、1.男、10.全部", example = "1", required = true, dataType = "int", paramType = "form"),
//			@ApiImplicitParam(name = "uuid", value = "MD5(随机数)", example = "27b8f8538f84fa456f783e600c27dd18", required = true, dataType = "String", paramType = "form"),
//			@ApiImplicitParam(name = "token", value = "MD5(rtype_sex_rtype_sex_uuid_rtype_time)", example = "1001", required = true, dataType = "String", paramType = "form") })
//	@CheckingMD5(checkingStr = "rtype_sex_rtype_sex_uuid_rtype", MD5 = "token")
//	public Object tjList(Integer rtype, Integer sex, String uuid, String token, Pages pages) {
//		if (rtype == null || (rtype != 0 && rtype != 1)) {
//			return new RSPE<Object>(-2, "应用平台参数错误！");
//		}
//		if (sex == null || (sex != 0 && sex != 1 && sex != 10)) {
//			return new RSPE<JDGoods>(-3, "性别参数错误！");
//		}
//		Callable<RSPE<ResultPage<JDGoods>>> result = new Callable<RSPE<ResultPage<JDGoods>>>() {
//			@Override
//			public RSPE<ResultPage<JDGoods>> call() throws Exception {
//				return new RSPE<ResultPage<JDGoods>>(jdShopsService.getTJList(rtype, sex, pages));
//			}
//		};
//		return result;
//	}
}
